package org.itli.jnsocket.Util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

    /**
     * 对字符串进行MD5加密
     *
     * @param input 需要加密的字符串
     * @return 加密后的字符串
     */
    public static String getMD5(String input) {
        try {
            // 获取MD5 MessageDigest 实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算MD5值
            md.update(input.getBytes());
            byte[] digest = md.digest();
            // 将字节转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            // 处理异常，实际应用中可能需要更详细的异常处理逻辑
            e.printStackTrace();
            return null;
        }
    }

    // 测试MD5加密
    public static void main(String[] args) {
        String originalString = "password123";
        String encryptedString = getMD5(originalString);
        System.out.println("Original: " + originalString);
        System.out.println("MD5 Encrypted: " + encryptedString);
    }
}